<?php

/**
 * Class SellerService
 */
class SellerService extends BaseService
{

    private static $self           =   NULL;
    private $mAd                    =   NULL;
    private $mNews                  =   NULL;
    private $mNewsCategory          =   NULL;



    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }
        return self::$self;
    }


    public function __construct()
    {

        $this->mAd                 =   new Involved_AdModel();
        $this->mNews               =   new Involved_NewsModel();
        $this->mNewsCategory       =   new Involved_NewsCategoryModel();

    }


    /**
     * 广告位列表
     */
    public function AdsList()
    {
        return $this->mAd->AdsList();
    }

    /**
     * 首页banner图
     */
    public function BannerIndex($type)
    {
        return $this->mAd->BannerIndex($type);
    }


    /**
     * 广告位添加
     * @param $paramObj
     * @return int
     */
    public function AdsAdd($paramObj)
    {
        if(!empty($paramObj->img_url))
            $paramObj->img_url =  QiniuUpload::instance()->upload_files($paramObj->img_url);
        #添加
        $aData = [
            'ad_name'          =>     $paramObj->ad_name,
            'img_url'          =>     $paramObj->img_url,
            'type'             =>     $paramObj->type,
            'is_rec'           =>     $paramObj->is_rec,
            'order_by'         =>     $paramObj->order_by,
            'link'             =>     $paramObj->link,
            'add_time'         =>     time(),
        ];
        //dd($aData);
        return $this->mAd->insert($aData);
    }

    /**
     * 广告位修改
     * @param $paramObj
     * @return bool
     * @throws Exception
     */
    public function AdsUp($paramObj)
    {
        #图片上传
        $paramObj->img_url = !empty($paramObj->img_url) ?
            $paramObj->img_url =  QiniuUpload::instance()->upload_files($paramObj->img_url):$paramObj->img_old;
        #修改
        $aData = [
            'ad_name'          =>     $paramObj->ad_name,
            'img_url'          =>     $paramObj->img_url,
            'type'             =>     $paramObj->type,
            'is_rec'           =>     $paramObj->is_rec,
            'order_by'         =>     $paramObj->order_by,
            'link'             =>     $paramObj->link,
            'add_time'         =>     time(),
        ];

        return $this->mAd->update($aData,['auto_id'=>$paramObj->auto_id]);

    }

    /**
     * 修改获取信息
     * @param $auto_id
     * @return mixed
     */
    public function GetAdsInfo($auto_id)
    {
        return $this->mAd->GetAdsInfo($auto_id);
    }

    /**
     * 删除广告
     * @param $paramObj
     * @return mixed
     */
    public function DelAds($paramObj)
    {
        return $this->mAd->delete(['auto_id'=>$paramObj->auto_id]);
    }

    /**
     * 删除ajax替换
     * @return mixed
     */
    public function AdsAjax()
    {
        return $this->mAd->AdsList();
    }







    /**
     * 文章列表
     * @return mixed
     */
    public function NewsList()
    {
        return $this->mNews->NewsSelect();
    }

    /**
     * 首页公告
     * @return mixed
     */
    public function NoticeList()
    {
        return $this->mNews->NoticeList();
    }

    /**
     * 首页媒体公告
     * @return mixed
     */
    public function IndexNewsList()
    {
        return $this->mNews->IndexNewsList();
    }


    /**
     * 文章添加
     * @param $paramObj
     * @return int
     */
    public function NewsAdd($paramObj)
    {
        #图片上传
        if(!empty($paramObj->thumb_url))
            $paramObj->thumb_url =  QiniuUpload::instance()->upload_files($paramObj->thumb_url);

        if(!empty($paramObj->media_logo))
            $paramObj->media_logo =  QiniuUpload::instance()->upload_files($paramObj->media_logo);

        #添加
        $aData = [
            'title'         => $paramObj->title,
            'content'       => $paramObj->content,
            'summary'       => $paramObj->summary,
            'cate_id'       => $paramObj->cate_id,
            'thumb_url'     => $paramObj->thumb_url,
            'media_logo'    => $paramObj->media_logo,
            'is_valid'      => $paramObj->is_valid,
            'media_source'  => $paramObj->media_source,
            'link'          => $paramObj->link,
            'admin_id'      => session_get(UserEnum::ADMIN_USER_KEY_ID),
            'add_time'      => time()
        ];
        return $this->mNews->insert($aData);
    }

    /**
     * 文章修改
     * @param $paramObj
     * @return bool
     */
    public function NewsUp($paramObj)
    {

        #图片上传
        $paramObj->thumb_url = !empty($paramObj->thumb_url) ?
            $paramObj->thumb_url =  QiniuUpload::instance()->upload_files($paramObj->thumb_url):$paramObj->img_old;

        $paramObj->media_logo = !empty($paramObj->media_logo) ?
            $paramObj->media_logo =  QiniuUpload::instance()->upload_files($paramObj->media_logo):$paramObj->logo_old;

        #修改
        $aData = [
            'title'         => $paramObj->title,
            'content'       => $paramObj->content,
            'summary'       => $paramObj->summary,
            'cate_id'       => $paramObj->cate_id,
            'thumb_url'     => $paramObj->thumb_url,
            'media_logo'    => $paramObj->media_logo,
            'is_valid'      => $paramObj->is_valid,
            'media_source'  => $paramObj->media_source,
            'link'          => $paramObj->link,
            'admin_id'      => session_get(UserEnum::ADMIN_USER_KEY_ID),
            'add_time'      => time()
        ];
        return $this->mNews->update($aData,['auto_id'=>$paramObj->auto_id]);
    }

    /**
     * 修改获取文章信息
     * @param $news_id
     * @return mixed
     */
    public function GetNewsId($news_id)
    {
        return $this->mNews->GetNewsId($news_id);
    }

    /**
     * 文章删除
     */
    public function NewsDel($aData)
    {
        return $this->mNews->delete(['auto_id'=>$aData->auto_id]);
    }

    /**
     * 删除ajax替换
     * @return mixed
     */
    public function NewsAjax()
    {
        return $this->mNews->NewsSelect();
    }

    /**
     * 文章详情ajax返回
     * @param $auto_id
     * @return mixed
     */
    public function NewsDetails($auto_id)
    {
        return $this->mNews->NewsDetails($auto_id);
    }

    /**
     * 文章轮播图启动
     * @param $auto_id
     * @param $is_carousel
     * @return bool
     */
    public function IsCarousel($auto_id,$is_carousel,$cate_id)
    {
        if($is_carousel==1){
            $aData = [
                'is_carousel' => '0',
            ];
           $this->mNews->update($aData,['auto_id'=>$auto_id]);
            if(empty($cate_id)){
                return $this->mNews->NewsSelect();
            }else{
                return $this->mNews->NewsCateShow($cate_id);
            }
        }else{
            $aData = [
                'is_carousel' => '1',
            ];
            $this->mNews->update($aData,['auto_id'=>$auto_id]);
            if(empty($cate_id)){
                return $this->mNews->NewsSelect();
            }else{
                return $this->mNews->NewsCateShow($cate_id);
            }
        }

    }

    /**
     * 文章分类显示
     * @param $cate_id
     */
     public function NewsCateShow($cate_id)
     {
         return $this->mNews->NewsCateShow($cate_id);
     }






    /**
     * 文章分类查询
     * @return mixed
     */
    public function Category()
    {
        return $this->mNewsCategory->Category();
    }

    /**
     * 文章分类列表
     * @return mixed
     */
    public function NewsCateList()
    {
        return $this->mNewsCategory->NewsCateList();
    }

    /**
     * 文章分类添加
     * @param $paramObj
     * @return int
     */
    public function NewsCateAdd($paramObj)
    {
        #添加
        $aData = [
            'name'          => $paramObj->name,
            'status'        => $paramObj->status,
            'add_time'      => time()
        ];

        return $this->mNewsCategory->insert($aData);

    }

    /**
     * 文章分类修改
     * @param $paramObj
     * @return bool
     */
    public function NewsCateUp($paramObj)
    {
        #添加
        $aData = [
            'name'          => $paramObj->name,
            'status'        => $paramObj->status,
            'add_time'      => time()
        ];

        return $this->mNewsCategory->update($aData,['auto_id'=>$paramObj->auto_id]);
    }

    /**
     * 修改获取信息
     * @param $auto_id
     * @return mixed
     */
    public function GetNewsCateId($auto_id)
    {
        return $this->mNewsCategory->GetNewsCateId($auto_id);
    }

    /**
     * 文章分类删除
     * @param $aData
     * @return int
     */
    public function NewsCateDel($aData)
    {
        return $this->mNewsCategory->delete(['auto_id'=>$aData->auto_id]);
    }

    /**
     * 删除ajax替换
     */
    public function NewsCateAjax()
    {
        return $this->mNewsCategory->NewsCateList();
    }
}


